package l1j.server.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.templates.Rune;
import l1j.server.server.utils.SQLUtil;
import l1j.server.server.utils.collections.Maps;

public class RuneTable {
	private static RuneTable _instance = new RuneTable();

	public static RuneTable getInstance() {
		return _instance;
	}

	private Map<Integer, Rune> _allRunes = Maps.newMap();

	private RuneTable() {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM x_runes");

			rs = pstm.executeQuery();
			while (rs.next()) {

				Rune r = new Rune();
				r.setHp(rs.getInt("hp"));
				r.setMp(rs.getInt("mp"));
				r.setHpr(rs.getInt("hpr"));
				r.setMpr(rs.getInt("mpr"));
				r.setStr(rs.getInt("str"));
				r.setDex(rs.getInt("dex"));
				r.setInt(rs.getInt("Int"));
				r.setCon(rs.getInt("con"));
				r.setWis(rs.getInt("wis"));
				r.setCha(rs.getInt("cha"));
				r.setAllAbility(rs.getInt("allAbility"));
				r.setDmgPercent(rs.getInt("dmgPercent"));
				r.setDrPercent(rs.getInt("drPercent"));
				r.setMaxTime(rs.getInt("maxTime"));
				r.setAc(rs.getInt("ac"));
				r.setMr(rs.getInt("mr"));

				_allRunes.put(rs.getInt("itemId"), r);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}

	public Rune getTemplate(int itemId) {
		if (_allRunes.containsKey(itemId)) {
			return _allRunes.get(itemId);
		}
		return Rune.NOTHING;
	}
}
